package com.ququ.syb.account.service;

import com.ququ.syb.account.dao.AccountInfoDao;
import com.ququ.syb.common.pojo.AccountInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import java.util.Date;
import java.util.List;

@Service
@EnableTransactionManagement
public class AccountInfoService {
    @Autowired
    private AccountInfoDao accountInfoDao;


    @Cacheable(value = "cache", key = "'accountinfo:'+#accountid", unless = "#result == null")
    public AccountInfo getAccountInfo(String accountid){
        return accountInfoDao.getAccountInfo(accountid);
    }

    @CacheEvict(value = "cache", key = "'accountinfo:'+#accountInfo.accountid")
    public Integer save(AccountInfo accountInfo){
        return accountInfoDao.save(accountInfo);
    }


    @CacheEvict(value = "cache", key = "'accountinfo:'+#accountid")
    public Integer save( String accountid,
                         String name,
                         String mobile,
                         String accountname,
                         String appcode,
                         String partner,
                         String idcardno,
                         Integer allowpay,
                         Integer allowwithdraw,
                         String token,
                         String password){
        AccountInfo accountInfo = new AccountInfo();
        accountInfo.setAccountid(accountid);
        accountInfo.setMobile(mobile);
        accountInfo.setName(name);
        accountInfo.setAccountname(accountname);
        accountInfo.setAppcode(appcode);
        accountInfo.setPartner(partner);
        accountInfo.setIdcardno(idcardno);
        accountInfo.setAllowpay(allowpay);
        accountInfo.setAllowwithdraw(allowwithdraw);
        accountInfo.setToken(token);
        accountInfo.setPassword(password);
        accountInfo.setCreatetime(new Date());
        accountInfo.setLastmodifytime(new Date());
        accountInfo.setStatus(1);
        return accountInfoDao.save(accountInfo);
    }
    
    @CachePut(value = "cache", key = "'accountinfo:'+#accountInfo.accountid")
    public AccountInfo update(AccountInfo accountInfo){
        accountInfoDao.update(accountInfo);
        return accountInfoDao.getEntity(accountInfo);
    }


    @CachePut(value = "cache", key = "'accountinfo:'+#accountid")
    public AccountInfo update(Integer id,
                         String accountid,
                         String name,
                         String mobile,
                         String accountname,
                         String appcode,
                         String partner,
                         String idcardno,
                         Integer allowpay,
                         Integer allowwithdraw,
                         String token,
                         String password,
                         Integer status){
        AccountInfo accountInfo = new AccountInfo();
        if (id == null){
            accountInfo= accountInfoDao.getAccountInfo(accountid);
        }else {
            accountInfo.setId(id);
            accountInfo.setAccountid(accountid);
        }
        accountInfo.setMobile(mobile);
        accountInfo.setName(name);
        accountInfo.setAccountname(accountname);
        accountInfo.setAppcode(appcode);
        accountInfo.setPartner(partner);
        accountInfo.setIdcardno(idcardno);
        accountInfo.setAllowpay(allowpay);
        accountInfo.setAllowwithdraw(allowwithdraw);
        accountInfo.setToken(token);
        accountInfo.setPassword(password);
        accountInfo.setLastmodifytime(new Date());
        accountInfo.setStatus(status);
        accountInfoDao.update(accountInfo);
        accountInfo = accountInfoDao.getAccountInfo(accountInfo.getAccountid());
        return accountInfo;
    }


    public List<AccountInfo> listAccountInfo(String mobile,String idcardno,Integer status){
        AccountInfo accountInfo = new AccountInfo();
        accountInfo.setMobile(mobile);
        accountInfo.setIdcardno(idcardno);
        accountInfo.setStatus(status);
        return accountInfoDao.listAccountInfo(accountInfo);
    }

}
